<template>
  <div class="user-list-container">
    <div class="data-table">
      <el-table
        :data="tableData"
        stripe
        border
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="username"
          align="center"
          label="用户名"
        ></el-table-column>
        <el-table-column
          prop="email"
          align="center"
          label="邮箱"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          align="center"
          label="手机号"
        ></el-table-column>
        <el-table-column
          prop="createdAt"
          sortable="custom"
          align="center"
          label="创建时间"
        ></el-table-column>
      </el-table>
    </div>
    <div
      align="right"
      class="paging-footer"
    >
      <el-pagination
        background
        @size-change="pageSizeChangeHandler"
        @current-change="currentPageChangeHandler"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :page-sizes="pageSizes"
        layout="total,sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui';
import { getUserListApi } from '../../../api/user';
import { MomentFormat, SortType } from '../../../constants/common';
import moment from 'moment';

export default {
  data() {
    return {
      tableData: [],
      totalCount: 0,
      currntPage: 1,
      pageSize: 10,
      pageSizes: [5, 10, 20, 50, 100],
      sort: {
        sortType: SortType.DESC,
        orderBy: 'createdAt'
      },
      selectedUserList: [],
      recipientUuidList: [],
      name: '',
      roleUuid: this.$route.query.uuid
    };
  },
  created() {
    this.search();
  },
  methods: {
    search() {
      getUserListApi(this.currntPage - 1, this.pageSize, this.sort.sortType, this.sort.orderBy, this.name, this.roleUuid, false)
        .then(({ data, total_count }) => {
          this.totalCount = total_count;
          this.tableData = data.map(selectedRoleList => {
            return {
              uuid: selectedRoleList.uuid,
              username: selectedRoleList.username,
              email: selectedRoleList.email,
              mobile: selectedRoleList.mobile,
              createdAt: moment(selectedRoleList.created_at).format(MomentFormat)
            };
          });
        })
        .catch(error => Message.error(error));
    },
    handleSortChange(res) {
      this.sort.sortType = res.order === 'descending' ? SortType.DESC : SortType.ASC;
      this.search();
    },
    handleSelectionChange(res) {
      this.selectedUserList = res;
      this.recipientUuidList = this.selectedUserList.map(item => item.uuid);
      this.$emit('recipient', this.recipientUuidList);
    }
  }
};
</script>

<style lang="scss" scoped>
.user-list-container {
  .data-table {
    padding: 10px;
    background-color: #fff;
  }
  padding: 10px;
  .local-name {
    font-size: 24px;
    font-weight: 800;
    padding: 10px 10px 20px 10px;
    .el-button--primary {
      float: right;
      background-color: #2c83b1 !important;
      border-color: #2c83b1 !important;
    }
  }
  .paging-footer {
    background-color: #fff;
    padding: 10px;
  }
}
</style>
